package com.radiant.data.entity;

import java.util.List;

import org.apache.commons.beanutils.ConvertUtilsBean;

import com.google.inject.Inject;
import com.radiant.data.entity.Activity.Type;
import com.radiant.data.entity.converter.SimpleDBEntityConverter;

public class ActivityDAOImpl extends SimpleDBDAO<Activity> implements ActivityDAO {

	@Inject
	MeasurementDAO measurementDAO;
	
	@Inject
	PostDAO postDAO;
	
	@Inject
	MilestoneDAO milestoneDAO;

	public ActivityDAOImpl() {
		super(Activity.class);
	}

	public List<Activity> getPersonalActivityFeed(String from, int count) {
		StringBuffer whereClause = new StringBuffer();
		whereClause.append("userName = '").append(from).append("'");
		whereClause.append(" AND creator = '").append(from).append("'");
		whereClause.append(" AND type in ('").append(Type.POST).append("',");
		whereClause.append("'").append(Type.MILESTONE).append("',");
		whereClause.append("'").append(Type.MEASUREMENT).append("'");
		whereClause.append(")");
		whereClause.append(" AND timeCreated is not null");
		return query(whereClause.toString(), "timeCreated", count, false);
	}
	
	public List<Activity> getActivityFeed(String from, int count) {
		StringBuffer whereClause = new StringBuffer();
		whereClause.append("userName = '").append(from).append("'");
		whereClause.append(" AND timeCreated is not null");
		return query(whereClause.toString(), "timeCreated", count, false);
	}

	@Override
	protected void registerConverters(ConvertUtilsBean convertUtilsBean) {
		convertUtilsBean.register(new SimpleDBEntityConverter<PostDAO>(preProcessor, postDAO), Post.class);
		convertUtilsBean.register(new SimpleDBEntityConverter<MeasurementDAO>(preProcessor, measurementDAO), Measurement.class);
		convertUtilsBean.register(new SimpleDBEntityConverter<MilestoneDAO>(preProcessor, milestoneDAO), Milestone.class);
	}

}
